package pdfsetter.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;

import pdfsetter.core.pages.PageInfo;
import pdfsetter.core.pages.PageRectangle;
import pdfsetter.core.pages.PageType;
import pdfsetter.core.schema.SinglePageSchema;
import pdfsetter.core.schema.advanced.AttachedFilePageSelector;
import pdfsetter.core.schema.advanced.ConsecutiveFileListProcessPattern;
import pdfsetter.core.schema.advanced.FillingSchemaPattern;
import pdfsetter.core.schema.selectors.NormalPageSelector;

public class AdvancedSettingExample2 {

    public static void main(String args[]) {
        System.out.println("Hello! This is simple advanced schemas setting example.");

        PageInfo a4page = new PageInfo(PageType.A4).rotate();
        ArrayList<PageRectangle> locations = new ArrayList<PageRectangle>();
        locations.add(new PageRectangle(0, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        locations.add(new PageRectangle(a4page.getWidth() / 2, 0, a4page.getWidth() / 2, a4page.getHeight(), 0));
        SinglePageSchema pageSchema = new SinglePageSchema("2xA5 on A4", a4page, locations);

        // ------------ Input Files --------------
        List<File> files = new LinkedList<File>();
        files.add(new File("src/test/resources/test-pdf-img.pdf"));

        // Load source documents and retrieve document sizes
        int[] fileSizes = new int[files.size()];
        int i = 0;
        for (File file : files) {
            try {
                PDDocument pdd = PDDocument.loadNonSeq(file, null);
                fileSizes[i] = pdd.getNumberOfPages();
                System.out.println("File info: " + fileSizes[i] + "(pages)");
                pdd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            i++;
        }
        // ------------ Use patterns and create and test FillingSchemas -------------
        ArrayList<AttachedFilePageSelector> asList = new ArrayList<AttachedFilePageSelector>();
        asList.add(new AttachedFilePageSelector(new NormalPageSelector(), 0)); // take every page from 0-file
        FillingSchemaPattern con1 = new ConsecutiveFileListProcessPattern(asList);
        AdvancedSettingExample.setCompileSave(pageSchema, con1.produceFillingSchema(fileSizes), files, "out-test/out-pdf-img.pdf");
    }
}
